package com.lzl.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lzl.eduservice.entity.EduChapter;
import com.lzl.eduservice.entity.EduVideo;
import com.lzl.eduservice.entity.chapter.ChapterVo;
import com.lzl.eduservice.entity.chapter.VideoVo;
import com.lzl.eduservice.mapper.EduChapterMapper;
import com.lzl.eduservice.service.EduChapterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzl.eduservice.service.EduVideoService;
import com.lzl.servicebase.exceptionHandler.LZLException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-03-30
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {

    @Autowired
    private EduVideoService eduVideoService;

    //课程大纲列表，根据课程id进行查询
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {

        //根据课程id查询课程里面所以的---章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);

        //根据课程id查询课程里面所以的---小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
        wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = eduVideoService.list(wrapperVideo);

        //创建list集合，封装最后得到的数据
        List<ChapterVo> findAllList = new ArrayList<>();

        //遍历查询得到的章节
        for (EduChapter eduChapter : eduChapterList) {
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);

            //创建集合，用户封装小节
            List<VideoVo> videoList = new ArrayList<>();
            //遍历所有小结，找到章节对应的小结
            for (EduVideo eduVideo : eduVideoList) {
                if(eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoList.add(videoVo);
                }
            }
            chapterVo.setChildren(videoList);
            findAllList.add(chapterVo);
        }

        return findAllList;
    }

    /**
     * 删除章节的第二种做法
     * 第二种：如果删除的章节下面有小节，不让进行删除---需要把小节删了才能删
     * @param chapterId
     */
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据chapter章节id  查询小节表，如果查询有数据-则不进行删除章节
        //需要用到小节的service方法
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        //现在不用查出数据---只需要得到有没有数据就行
//        List<EduVideo> list = eduVideoService.list(wrapper);
        int count = eduVideoService.count(wrapper);
        if(count>0){
            throw new LZLException(20001,"还有小节，不能直接删除");
        }else {
            int i = baseMapper.deleteById(chapterId);
            //简写方式
            return i>0;
        }
    }

    //根据课程id  删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
